package com.proxy.cglib;

import java.lang.reflect.Method;

import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;

public class BeanMapDemo {

	public static void main(String[] args) throws Exception {
		//BeanMap类实现了Java Map，将一个bean对象中的所有属性转换为一个String-to-Obejct的Java Map
		//我们使用BeanGenerator生成了一个含有两个属性的Java Bean，对其进行赋值操作后，生成了一个BeanMap对象，通过获取值来进行验证
		BeanGenerator generator = new BeanGenerator();
	    generator.addProperty("username", String.class);
	    generator.addProperty("password", String.class);
	    Object bean = generator.create();
	    Method setUserName = bean.getClass().getMethod("setUsername", String.class);
	    Method setPassword = bean.getClass().getMethod("setPassword", String.class);
	    setUserName.invoke(bean, "admin");
	    setPassword.invoke(bean, "password");
	    BeanMap map = BeanMap.create(bean);
	    System.out.println(map.get("username"));
	    System.out.println(map.get("password"));
	}

}
